﻿using MemberManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace MemberManager.Common.Classes
{
    public class AccountHelper:BaseHelper
    {
        public List<AccountInfo> GetList(string domainName)
        {
            //获取指定域的所有帐号列表返回

            if (_application == null || _application.Domains == null || _application.Domains.Count == 0)
            {
                return null;
            }
            else if (_application.Domains == null || _application.Domains.Count == 0)
            {
                return null;
            }

            hMailServer.Domain domain = null;
            try
            {
                domain = _application.Domains.ItemByName[domainName];
            }
            catch { }
            if (domain == null)
                return null;

            if (domain.Accounts == null || domain.Accounts.Count == 0)
                return null;

            List<AccountInfo> list = new List<AccountInfo>();

            for(int x = 0;x<domain.Accounts.Count;x++)
            {
                list.Add(new AccountInfo() {
                    AccountName = domain.Accounts[x].Address,
                    AccountPwd = "",
                    DomainName = domainName,
                    IsDefaultFloder = CheckHasDefaultFloder(domain.Accounts[x])
                });
            }

            return list;

        }

        /// <summary>
        /// 查看是否是默认文件夹
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        private bool CheckHasDefaultFloder(hMailServer.Account account)
        {
            string[] str = new string[] { "INBOX", "Drafts", "Sent", "Spam", "Trash" };

            if(account.IMAPFolders.Count>=5)
            {
                for(int x=  0;x< str.Length;x++)
                {
                    try
                    {
                        hMailServer.IMAPFolder folder = account.IMAPFolders.ItemByName[str[x]];
                    }
                    catch
                    {
                        return false;
                    }
                }

                return true;
            }
            return false;
        }


        public bool AddAccount(string domainName, string accountName, string accountPwd, hMailServer.eAdminLevel adminLevel = hMailServer.eAdminLevel.hAdminLevelNormal, bool isCreateDefaultFolder = false)
        {
            try
            {
                if (_application == null || _application.Domains == null || _application.Domains.Count == 0)
                {
                    return false;
                }
                else if (_application.Domains == null || _application.Domains.Count == 0)
                {
                    return false;
                }

                hMailServer.Domain domain = null;
                try
                {
                    domain = _application.Domains.ItemByName[domainName];
                }
                catch{}
                if (domain == null)
                    return false;

                hMailServer.Account account = null;
                //判断帐号是否存在
                if (domain.Accounts != null && domain.Accounts.Count > 0)
                {
                    try
                    {
                        account = domain.Accounts.ItemByAddress[domainName + "@" + accountName];
                    }
                    catch { }
                    if (account != null)
                        return false;
                }

                account = domain.Accounts.Add();
                
                Marshal.ReleaseComObject(domain);


                account.Address = accountName + "@" + domainName;

                account.MaxSize = 0;
                account.Active = true;

                account.VacationMessageIsOn = false;
                account.VacationSubject = "";
                account.VacationMessage = "";
                account.VacationMessageExpires = false;


                account.ForwardEnabled = false;
                account.ForwardAddress = "";
                account.ForwardKeepOriginal = false;


                account.SignatureEnabled = false;
                account.SignaturePlainText = "";
                account.SignatureHTML = "";

                account.IsAD = false;
                account.ADDomain = "";
                account.ADUsername = "";

                account.PersonFirstName = "";
                account.PersonLastName = "";
                account.AdminLevel = adminLevel;

                account.Password = accountPwd;

                account.SignatureEnabled = false;
                account.Save();

                if(isCreateDefaultFolder)
                {
                    if(CreateFolder(account, new string[] { "Drafts", "Sent", "Spam", "Trash" }, domainName, accountName))
                    {
                        return false;
                    }
                }


                return true;
            }
            catch (Exception ex)
            {
                return false;
            }


        }


        private bool CreateFolder(hMailServer.Account account, string[] floderNames, string domainName, string accountName)
        {
            try
            {
                foreach (string floderName in floderNames)
                {
                    hMailServer.IMAPFolders _folders = account.IMAPFolders;

                    hMailServer.IMAPFolder folder = _folders.Add(floderName);
                    folder.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}
